<template>
  <section>
    <div class="cenTopBox">
      <div class="todayTotal">
        <span>当日交易金额</span>
        <span>{{total_pay_charge}} 元</span>
      </div>
      <div class="othTotal" style="left:0rem;">
        <p>{{entity.total_space_count}}</p>
        <p>累计总车位数</p>
      </div>
      <div class="othTotal" style="left:1.8rem;">
        <p>{{entity.total_free_space_count}}</p>
        <p>当前剩余车位数</p>
      </div>
      <div class="othTotal" style="left:3.6rem;">
        <p>{{entity.total_pay_count}}</p>
        <p>当日交易笔数</p>
      </div>
      <div class="othTotal" style="left:5.4rem;">
        <p>{{entity.total_register_count}}</p>
        <p>累计注册用户数</p>
      </div>
    </div>
  </section>
</template>

<script>
import {
  getSpaceChargeMsg
} from '../api/api';
export default {
  data() {
    return {
      msg: '中间顶部',
      entity: '',
      total_pay_charge: ''
    }
  },
  mounted() {
    this.getSpaceChargeMsg();
    setInterval(() => {
      this.getSpaceChargeMsg();
    }, 5000);

  },
  methods: {
    getSpaceChargeMsg() {
      getSpaceChargeMsg().then(res => {
        this.entity = res;
        this.formatMoney(res.total_pay_charge)
        console.log(this.formatMoney('123123.33'))
        console.log(this.formatMoney(res.total_pay_charge))
        this.total_pay_charge = this.formatMoney(res.total_pay_charge)
      })
    },
    //格式化金额
    formatMoney(s) {
      if (/[^0-9\.]/.test(s)) return "invalid value";
      s = s.replace(/^(\d*)$/, "$1.");
      s = (s + "00").replace(/(\d*\.\d\d)\d*/, "$1");
      s = s.replace(".", ",");
      var re = /(\d)(\d{3},)/;
      while (re.test(s))
        s = s.replace(re, "$1,$2");
      s = s.replace(/,(\d\d)$/, "");
      return "￥" + s.replace(/^\./, "0.")
    }

  }
}
</script>

<style scoped>
.cenTopBox {
  position: absolute;
  left: 5.5rem;
  top: 0.9rem;
  width: 7.2rem;
  height: 1.8rem;
  color: #ff0;
  font-size: 0.3rem;
}

.cenTopBox .todayTotal {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 6rem;
  height: 0.6rem;
  background: url(../assets/bgcentop01.png) no-repeat left bottom;
  text-align: left;
}
.cenTopBox .todayTotal span {
  font-size: 0.3rem;
  color: #e6d44f;
  position: absolute;
  left: 1.9rem;
  top: 0.12rem;
}
.cenTopBox .todayTotal span:first-child {
  font-size: 0.24rem;
  color: #55d6e9;
  left: 0.2rem;
  top: 0.12rem;
}

.othTotal {
  position: absolute;
  left: 0;
  top: 0.7rem;
  width: 1.65rem;
  height: 1rem;
  background: url(../assets/bgcentop02.png) no-repeat left bottom;
  text-align: left;
}

.othTotal p {
  text-align: center;
  font-size: 0.26rem;
  padding-top: 0.18rem;
  line-height: 0.3rem;
  color: #e6d44f;
  margin: 0;
}
.othTotal p:last-child {
  padding-top: 0.1rem;
  font-size: 0.2rem;
  color: #55d6e9;
}
</style>
